package com.wei.duowan.bbs.utils;

import android.content.Context;
import android.os.Environment;
import android.support.annotation.Nullable;

import com.wei.duowan.bbs.App;

import java.io.File;

/**
 * 作者：赵若位
 * 时间：2019/8/21 21:52
 * 联系方式：1070138445
 * 功能：
 */
public class CacheFileUtils
{

    private static CacheFileUtils sCacheFileUtils;
    private Context mContext;
    private File mCacheFile;
    /*外部存储是否具有权限*/
    private Boolean mIsPermission = false;


    private CacheFileUtils()
    {
        mContext = App.getContext();
        init();
    }


    public static CacheFileUtils getInstance()
    {
        if (sCacheFileUtils == null)
        {
            synchronized (CacheFileUtils.class)
            {
                if (sCacheFileUtils == null)
                {
                    sCacheFileUtils = new CacheFileUtils();
                }
            }
        }
        return sCacheFileUtils;
    }


    private void init()
    {
        mCacheFile = (mIsPermission && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) ?
                new File(Environment.getExternalStorageDirectory(), mContext.getPackageName()) : mContext.getCacheDir();
    }

    public CacheFileUtils setCacheFile(@Nullable File file)
    {
        if (file != null)
        {
            this.mCacheFile = file;
        }
        return this;
    }

    public File getCacheFile()
    {
        return mCacheFile;
    }


    public CacheFileUtils setIsPermission(Boolean permission)
    {
        this.mIsPermission = permission;
        init();
        return this;
    }
}
